package com.example.chao.myphotosearch.view.activities;

import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.RelativeLayout;

import com.example.chao.myphotosearch.R;
import com.example.chao.myphotosearch.view.fragments.SearchFragment;
import com.facebook.drawee.view.SimpleDraweeView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by chao on 2016/4/7.
 */
public class SearchActivity extends AppCompatActivity {
    @Bind(R.id.bg_img)
    SimpleDraweeView bgImg;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.collapsingToolbarLayout)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @Bind(R.id.search_app_bar)
    AppBarLayout searchAppBar;
    @Bind(R.id.search_container)
    RelativeLayout searchContainer;
    @Bind(R.id.floating_action_button)
    FloatingActionButton floatingActionButton;
//    @Bind(R.id.search_fab)
//    FloatingActionButton searchFab;
    private SearchFragment fragment;

//    @Bind(R.id.iv)
//    ImageView iv;
//    @Bind(R.id.toolbar_back_IV)
//    ImageView toolbarBackIV;
//    @Bind(R.id.title_TV)
//    TextView titleTV;
//    @Bind(R.id.toolbar_back_IV)
//    ImageView toolbarBackIV;
//    ViewGroup.LayoutParams params;
//    private int height;
//    private int width;
//    private int searchBarHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        toolbar.setTitle(getIntent().getBundleExtra("search").getString("title"));
        onSetToolbar(toolbar);
//        initAppBarSetting();
//        collapsingToolbarLayout.setTitle(getIntent().getBundleExtra("search").getString("search"));
//        collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);
//        collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);
//        collapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);//设置还没收缩时状态下字体颜色
//        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.GREEN);//设置收缩后Toolbar上字体的颜色
//        initParams();
        if(getIntent().getBundleExtra("search").getString("bg_img")==null) {
            bgImg.setImageResource(R.drawable.ic_launcher);
        }else {
            bgImg.setImageURI(Uri.parse(getIntent().getBundleExtra("search").getString("bg_img")));
        }
//        titleTV.setText(getIntent().getBundleExtra("search").getString("search"));
        fragment = new SearchFragment();
        fragment.setArguments(getIntent().getBundleExtra("search"));
        getSupportFragmentManager().beginTransaction().replace(R.id.search_container, fragment).commit();
    }

//    private void initParams(){
//        params = iv.getLayoutParams();
//        height = 300;
//        width = 300;
//        searchBarHeight = 700;
//    }

    //    @OnClick(R.id.toolbar_back_IV)
//    public void backIvClick(View view){
//        finish();
//    }
    public void onSetToolbar(Toolbar toolbar) {
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    public void initAppBarSetting() {
        searchAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int i) {
//                MainFragment fragment = (MainFragment) adapter.getFragment(0);
//                fragment.setMainSwipeEnable(i);
//                if (!floatingActionButton.isShown()) {
////                    collapsingToolbarLayout.setTitle(getIntent().getBundleExtra("search").getString("search"));
//                    titleTV.setVisibility(View.VISIBLE);
//                } else {
//                    titleTV.setVisibility(View.GONE);
//                }
//                params.height = height+(height*i)/700;
//                params.width = width+(width*i)/700;
//                iv.setLayoutParams(params);
//                if (i != 0) {
//                    searchFab.hide();
//                } else {
//                    searchFab.show();
//                }
            }
        });
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }
}
